package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.DeptUserResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.MainView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainPrensenter extends BasePresenter<MainView> {
    private static final String TAG = "MainPrensenter:";
    public MainPrensenter(BaseActivity context) {

        super(context);
    }
   //查看是普通人员还是部门领导
    public void getDeptUserList(){
        LogUtils.v(TAG + "getDeptUserList");
        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().getDeptUserList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( deptuserresponse-> {
                    String success = deptuserresponse.getMeta().getSuccess();
                    mContext.hideWaitingDialog();

                    if (null != success && Boolean.parseBoolean(success)) {

                        List<DeptUserResponse.Data> deptuserresponseList = deptuserresponse.getData();
                        LogUtils.v(TAG + deptuserresponseList.toString());
                        getView().getDeptUserList(deptuserresponseList);

                    }

                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        getView().getDeptUserList(null);
        mContext.hideWaitingDialog();
    }
}
